package com.wr.controller;

import com.wr.service.EmailService;
import com.wr.service.SessionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


@Controller
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.wr")
public class HelloController {
    private static Logger log = LoggerFactory.getLogger(HelloController.class);

    private EmailService emailService;

    private SessionService sessionService;

    @Autowired
    public HelloController(EmailService emailService, SessionService sessionService) {
        this.emailService = emailService;
        this.sessionService = sessionService;
    }


    @RequestMapping("/hello")
    public String hello(Map<String,Object> map){
        map.put("date",new Date());
        map.put("message","Hello World!");
        map.put("code",emailService.selectByEmail("529330509@qq.com").getCode());
        log.info("hello请求处理完成");
        return "hello";
    }

    @RequestMapping("/loginPage")
    public String loginPage(@ModelAttribute("error") String errorMsg, Map<String,Object> map){
        if(errorMsg != null && !"".equals(errorMsg)){
            map.put("errorMsg",errorMsg);
        }
        log.info("loginPage请求处理完成");
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String mobile, String password, RedirectAttributes redirectAttributes){
        String result;
        if(mobile != null && !"".equals(mobile) && mobile.equals("15140230115")){
            if(password != null && !"".equals(password) && password.equals("19930608")){
                request.getSession().setAttribute("mobile",mobile);
                log.info("login请求处理完成");
                result =  "redirect:hello";
            }else{
                redirectAttributes.addFlashAttribute("error","手机号或密码错误!");
                result = "redirect:loginPage";
            }
        }else{
            redirectAttributes.addFlashAttribute("error","手机号或密码错误!");
            result = "redirect:loginPage";
        }
        log.info("login请求处理完成");
        return result;
    }

    @RequestMapping("/loginOut")
    public String outLogin(HttpServletRequest request){
        String mobile = (String) request.getSession().getAttribute("mobile");
        if(mobile != null && !"".equals(mobile)){
            sessionService.deleteSessionByMobile(mobile);
        }
        request.getSession().removeAttribute("mobile");
        return "redirect:loginPage";
    }
}
